using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
namespace MicroMsg.UI.Page
{
	public class BottlesListPage : ChatContainerPage
	{
		public enum BottleListPageEntryType
		{
			fromConvList,
			fromDriftBottleMain
		}
		private const string TAG = "BottleListPage";
		private const string ScrollingState = "Scrolling";
		private const string NotScrollingState = "NotScrolling";
		private MMObservableCollection<BConversation, BottleSessionItem> mDataList = new MMObservableCollection<BConversation, BottleSessionItem>();
		private EventWatcher mListChangeWatcher;
		private ScrollViewer mScrollViewer;
		private bool mBIsScrollHandlerSet;
		private ApplicationBar mBar;
		private ApplicationBarMenuItem pluginSettingButton;
		private ApplicationBarMenuItem clearBConvButton;
		private List<UIElement> mUiToHideList;
		internal Grid LayoutRoot;
		internal Grid LayoutContent;
		internal panelTitle2Line panelTitle;
		internal StackPanel listView;
		internal Grid ContentPanel;
		internal MMListBox BottleSessionlist;
		internal Grid NoMemberView;
		private bool _contentLoaded;
		public bool IsScrolling
		{
			get;
			private set;
		}
		public BottlesListPage()
		{
			base.add_Loaded(new RoutedEventHandler(this.BottleListPage_loaded));
			base.add_Unloaded(new RoutedEventHandler(this.BottleListPage_unloaded));
			this.InitializeComponent();
		}
		private void BottleListPage_loaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("BottleListPage", "BottleListPage page loaded");
			this.InitWatcher();
			this.setScrollHandler();
			this.requestHeadImgInCurrView();
		}
		private void BottleListPage_unloaded(object sender, RoutedEventArgs e)
		{
		}
		protected override void OnPageIn(object[] object_0)
		{
			Log.smethod_5("BottleListPage", "BottleListPage page in");
			if (object_0 == null || Enumerable.Count<object>(object_0) == 1)
			{
			}
			this.InitBottleSessionView();
		}
		private void InitWatcher()
		{
			if (this.mListChangeWatcher == null)
			{
				this.mListChangeWatcher = new EventWatcher(this, this, new EventHandlerDelegate(this.OnBottleConversationChange));
				EventCenter.registerEventWatcher(EventConst.ON_BCONVERSATION_LIST_CHANGE, this.mListChangeWatcher);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_BCONVERSATION_DEL, this.mListChangeWatcher);
			}
		}
		private void UnitWatcher()
		{
			if (this.mListChangeWatcher != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_BCONVERSATION_LIST_CHANGE, this.mListChangeWatcher);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_BCONVERSATION_DEL, this.mListChangeWatcher);
				this.mListChangeWatcher = null;
			}
		}
		private void OnBottleConversationChange(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			List<BConversation> listObject = evtArgs.getListObject<BConversation>();
			if (listObject == null)
			{
				return;
			}
			using (List<BConversation>.Enumerator enumerator = listObject.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					BConversation current = enumerator.get_Current();
					if (current != null)
					{
						if (evtArgs.mEventID == EventConst.ON_BCONVERSATION_LIST_CHANGE)
						{
							this.ConversationUpdate(current, false);
						}
						else
						{
							if (evtArgs.mEventID == EventConst.ON_STORAGE_BCONVERSATION_DEL)
							{
								Log.smethod_5("BottleListPage", "ConversationDelete In Ui List" + current.strUsrName);
								this.ConversationDelete(current.strUsrName);
							}
						}
					}
				}
			}
		}
		private void InitBottleSessionView()
		{
			Log.smethod_5("BottleListPage", "ServiceCenter.bconversationMgr.getList() start");
			this.ConversationUpdate(ServiceCenter.bconversationMgr.getList());
			Log.smethod_5("BottleListPage", "ServiceCenter.qconversationMgr.getList() end");
			this.mDataList.Bind(this.BottleSessionlist);
			Log.smethod_5("BottleListPage", "Data bind to UI end");
			if (this.mDataList.get_Count() <= 0)
			{
				Log.smethod_5("BottleListPage", "mDataList.Count <= 0 No Data");
				this.ChangeToNoMsgView();
				return;
			}
			this.ChangeToListView();
		}
		private void ConversationUpdate(ObservableCollection<BConversation> cvsList)
		{
			if (cvsList == null)
			{
				return;
			}
			using (IEnumerator<BConversation> enumerator = cvsList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					BConversation current = enumerator.get_Current();
					this.ConversationUpdate(current, true);
				}
			}
		}
		public void ConversationUpdate(BConversation bconversation_0, bool isUpdateFromList = false)
		{
			if (bconversation_0 == null)
			{
				return;
			}
			bool flag = true;
			for (int i = 0; i < this.mDataList.get_Count(); i++)
			{
				if (this.mDataList[i].strUsrName == bconversation_0.strUsrName)
				{
					this.mDataList.RemoveAt(i);
					IL_48:
					if (flag)
					{
						int num = this.mDataList.get_Count();
						int j = 0;
						while (j < this.mDataList.get_Count())
						{
							if (bconversation_0.nTime <= this.mDataList[j].nTime)
							{
								j++;
							}
							else
							{
								num = j;
								IL_8A:
								this.mDataList.Insert(num, bconversation_0);
								if (this.mDataList.get_Count() > 0 && this.listView.get_Visibility() == 1)
								{
									this.ChangeToListView();
									return;
								}
								return;
							}
						}
						goto IL_8A;
					}
					return;
				}
			}
			goto IL_48;
		}
		public void ConversationDelete(string userName)
		{
			for (int i = 0; i < this.mDataList.get_Count(); i++)
			{
				BConversation bConversation = this.mDataList[i];
				if (bConversation.strUsrName == userName)
				{
					this.mDataList.RemoveAt(i);
					IL_3F:
					if (this.mDataList.get_Count() == 0)
					{
						this.ChangeToNoMsgView();
					}
					return;
				}
			}
			goto IL_3F;
		}
		private void BottleSessionItem_ItemClick(object sender, object object_0)
		{
			BConversation bConversation = object_0 as BConversation;
			if (bConversation == null)
			{
				return;
			}
			List<string> list = new List<string>();
			list.Add(BContactMgr.toBottleContactUserName(bConversation.strUsrName));
			ServiceCenter.sceneBatchGetContact.doScene(list);
			Log.smethod_5("BottleListPage", "BottleItem Click, sceneBatchGetContact.doScene(contactList)");
			ChatContainerPage.GoToChatPage(this, bConversation.strUsrName, null, null, null);
		}
		private void setScrollHandler()
		{
			if (this.mBIsScrollHandlerSet)
			{
				return;
			}
			this.mBIsScrollHandlerSet = true;
			this.mScrollViewer = (ScrollViewer)UIElementTraveler.FindElementRecursive(this.BottleSessionlist, typeof(ScrollViewer));
			if (this.mScrollViewer == null)
			{
				return;
			}
			FrameworkElement frameworkElement = VisualTreeHelper.GetChild(this.mScrollViewer, 0) as FrameworkElement;
			if (frameworkElement != null)
			{
				VisualStateGroup visualStateGroup = UIElementTraveler.FindVisualState(frameworkElement, "ScrollStates");
				if (visualStateGroup != null)
				{
					visualStateGroup.add_CurrentStateChanging(new EventHandler<VisualStateChangedEventArgs>(this.OnScrollStateChanging));
				}
			}
		}
		private void OnScrollStateChanging(object sender, VisualStateChangedEventArgs e)
		{
			this.IsScrolling = (e.get_NewState().get_Name() == "Scrolling");
			if (e.get_NewState().get_Name() == "NotScrolling")
			{
				if (this.mScrollViewer != null)
				{
					this.mScrollViewer.ScrollToVerticalOffset(this.mScrollViewer.get_VerticalOffset());
				}
				this.requestHeadImgInCurrView();
				this.BottleSessionlist.UpdateLayout();
			}
		}
		private void requestHeadImgInCurrView()
		{
			if (this.mScrollViewer != null && this.mDataList != null)
			{
				Log.smethod_5("BottleListPage", "requestHeadImgInCurrView start");
				int num = 6;
				int num2 = (int)this.mScrollViewer.get_VerticalOffset();
				num2 = num2;
				if (num2 < 0)
				{
					num += num2;
					num2 = 0;
				}
				int i = num2;
				int num3 = 0;
				while (i < this.mDataList.get_Count())
				{
					string gparam_ = BContactMgr.toBottleContactUserName(this.mDataList[i].strUsrName);
					BottleContact bottleContact = StorageMgr.bContact.method_0(gparam_);
					if (bottleContact != null)
					{
						HeadImgMgr.getHeadImage(bottleContact);
					}
					i++;
					num3++;
					if (num3 == num)
					{
						break;
					}
				}
				Log.smethod_5("BottleListPage", "requestHeadImgInCurrView end");
				return;
			}
		}
		public override void setAppBarStyle(IApplicationBar appbar)
		{
			base.setAppBarStyle(appbar);
		}
		public ApplicationBar GetApplicationBarBase()
		{
			if (this.mBar != null && this.mBar.get_MenuItems().get_Count() == 1)
			{
				if (!this.mBar.get_IsVisible())
				{
					this.mBar.set_IsVisible(true);
				}
				return this.mBar;
			}
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			this.mBar.set_Mode(1);
			this.setAppBarStyle(this.mBar);
			this.pluginSettingButton = new ApplicationBarMenuItem(strings.Setting_PluginsReadBottleMsg);
			this.pluginSettingButton.add_Click(new EventHandler(this.pluginSettingButton_Click));
			this.mBar.get_MenuItems().Add(this.pluginSettingButton);
			return this.mBar;
		}
		public ApplicationBar GetApplicationBarBaseAndDel()
		{
			if (this.mBar != null && this.mBar.get_MenuItems().get_Count() == 2)
			{
				if (!this.mBar.get_IsVisible())
				{
					this.mBar.set_IsVisible(true);
				}
				return this.mBar;
			}
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			this.mBar.set_Mode(1);
			this.setAppBarStyle(this.mBar);
			this.clearBConvButton = new ApplicationBarMenuItem(strings.Float_Bottle_ClearBottle);
			this.clearBConvButton.add_Click(new EventHandler(this.clearBConvButton_Click));
			this.mBar.get_MenuItems().Add(this.clearBConvButton);
			this.pluginSettingButton = new ApplicationBarMenuItem(strings.Setting_PluginsReadBottleMsg);
			this.pluginSettingButton.add_Click(new EventHandler(this.pluginSettingButton_Click));
			this.mBar.get_MenuItems().Add(this.pluginSettingButton);
			return this.mBar;
		}
		private void clearBConvButton_Click(object sender, EventArgs e)
		{
			MMMessageBox mMMessageBox = MMMessageBox.Show(strings.Float_Bottle_ClearBottle, strings.Float_Bottle_AlertContent_ClearBottle, true, strings.COM_Confirm, strings.COM_Cancel);
			mMMessageBox.ReturnEvent = new DlgReturnEvent(this.OnClearBottleMsgHandler);
		}
		private void OnClearBottleMsgHandler(DlgReturnEventArg dlgReturnEventArg_0)
		{
			switch (MMMessageBox.GetUserClickResult(dlgReturnEventArg_0))
			{
			case 0:
			{
				this.showProgressCover(strings.COM_Clearing);
				Dispatcher arg_45_0 = Deployment.get_Current().get_Dispatcher();
				Delegate arg_45_1 = new EventHandler(this.OnDoClearEvent);
				object[] array = new object[2];
				array[0] = this;
				arg_45_0.BeginInvoke(arg_45_1, array);
				break;
			}
			case 1:
				break;
			default:
				return;
			}
		}
		public void OnDoClearEvent(object sender, EventArgs e)
		{
			this.mDataList.Clear();
			BConversationMgr.DriftBottleClearAll();
			this.hideProgressCover();
		}
		private void pluginSettingButton_Click(object sender, EventArgs e)
		{
			base.GoToPage("/Source/Plugin/DriftBottle/UI/DriftBottleMainPage.xaml", null, false);
			for (int i = 0; i < PageManager.getInstance().basePageList.get_Count(); i++)
			{
				if (PageManager.getInstance().basePageList.get_Item(i) is DriftBottleMainPage)
				{
					PageManager.getInstance().PopPage();
					return;
				}
			}
		}
		private void ChangeToNoMsgView()
		{
			if (this.NoMemberView.get_Visibility() == null)
			{
				return;
			}
			this.listView.set_Visibility(1);
			this.NoMemberView.set_Visibility(0);
			base.set_ApplicationBar(this.GetApplicationBarBase());
		}
		private void ChangeToListView()
		{
			if (this.listView.get_Visibility() == null)
			{
				return;
			}
			this.NoMemberView.set_Visibility(1);
			this.listView.set_Visibility(0);
			base.set_ApplicationBar(this.GetApplicationBarBaseAndDel());
		}
		public override List<UIElement> GetBkContents()
		{
			if (this.mUiToHideList == null)
			{
				this.mUiToHideList = new List<UIElement>();
				this.mUiToHideList.Add(this.LayoutContent);
			}
			return this.mUiToHideList;
		}
		public override Panel GetChatContainerPanel()
		{
			return this.LayoutRoot;
		}
		protected override void resumeFromChat()
		{
			this.BottleSessionlist.Focus();
			UIDelayService.getInstance().DoDelayAction();
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
			if (base.PreOnBackKeyPress(cancelEventArgs_0))
			{
				return;
			}
			if (!cancelEventArgs_0.get_Cancel())
			{
				cancelEventArgs_0.set_Cancel(true);
				ItemFlyInAndOutAnimations.TitleFlyOut(this);
				base.get_Dispatcher().BeginInvoke(delegate
				{
					base.GoBack();
				});
			}
		}
		protected override void OnPageOut()
		{
			base.PreOnPageOut();
			this.UnitWatcher();
		}
		protected override void OnPageForward(Uri targetPage)
		{
			base.PreOnPageForward(targetPage);
		}
		protected override void OnPageBack()
		{
			base.PreOnPageBack();
		}
		protected override bool IsAlertMsgNeedShow(ChatMsg chatMsg_0)
		{
			bool result = true;
			if (base.PreIsAlertMsgNeedShow(chatMsg_0, ref result))
			{
				return result;
			}
			return !ContactHelper.isBottleContact(chatMsg_0.strTalker);
		}
		private void showProgressCover(string strCoverTitle)
		{
			ProcessingCover.getProgressBar(this.LayoutRoot, strCoverTitle, null, false, false, null);
		}
		private void hideProgressCover()
		{
			ProcessingCover.releaseProgressBar(this.LayoutRoot);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/DriftBottle/UI/BottlesListPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.LayoutContent = (Grid)base.FindName("LayoutContent");
			this.panelTitle = (panelTitle2Line)base.FindName("panelTitle");
			this.listView = (StackPanel)base.FindName("listView");
			this.ContentPanel = (Grid)base.FindName("ContentPanel");
			this.BottleSessionlist = (MMListBox)base.FindName("BottleSessionlist");
			this.NoMemberView = (Grid)base.FindName("NoMemberView");
		}
	}
}
